package com.example.plugin.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Random;
import java.util.zip.CRC32;

/**
 * 字节操作类
 */
public class ByteUtil {
    /**
     * 生成随机byte数组
     * @return
     */
    public static byte[] getRanByteArray(int n){
        Random random=new Random();
        byte[] bytes=new byte[n];
        //StringBuilder sb=new StringBuilder();
        for(int i=0;i<n;i++) {
            int r = random.nextInt(256);
           bytes[i]=(byte)r;
        }
        return bytes;
    }
    /**
     * 截取byte数组
     */
    public static byte[] subBytes(byte[] bytes,int start,int length){
        byte[] buf=new byte[length];
        System.arraycopy(bytes,start,buf,0,length);
        return buf;
    }

    /**
     * 截取int数组
     */
    public static int[] subInts(int[] ints,int start,int length){
        int[] buf=new int[length];
        System.arraycopy(ints,start,buf,0,length);
        return buf;
    }


    public static byte[] getCrc32(byte[] bytes){
        CRC32 crc32=new CRC32();
        crc32.update(bytes);
        long l=crc32.getValue();
        byte[] ret=Converter.hexStr2ByteArray(Converter.long2Hex(l));
        return ret;
    }
    /**
     * 反取crc32
     * @param bytes
     * @return
     */
    public static byte[] getRevCrc32(byte[] bytes){
        CRC32 crc32=new CRC32();
        byte[] ret=new byte[4];
        crc32.update(bytes);
        long l=crc32.getValue();
        byte[] buf=Converter.hexStr2ByteArray(Converter.long2Hex(l));
        for(int i=0;i<buf.length;i++){
            ret[i]=buf[buf.length-i-1];
        }
        return ret;
    }

    /**
     * 查找指定byte数组，返回索引
     * @return
     */
    public static int byteIndexOf(byte[] srcBytes,byte[] searchBytes){
        for (int i = 0; i < srcBytes.length - searchBytes.length+1; i++)
        {
            if (srcBytes[i] == searchBytes[0])
            {
                if (searchBytes.length == 1) { return i; }
                boolean flag = true;
                for (int j = 1; j < searchBytes.length; j++)
                {
                    if (srcBytes[i + j] != searchBytes[j])
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag) { return i; }
            }
        }
        return -1;
    }
    public static byte[] inputStream2ByteArray(InputStream inputStream){
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int rc = 0;
            while ((rc = inputStream.read(buf, 0, 1024)) > 0) {
                outputStream.write(buf, 0, rc);
            }
            byte[] bin = outputStream.toByteArray();
            return bin;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String splitHex(String hex){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<hex.length()/2;i++){
            sb.append(hex.substring(i*2,i*2+2));
            sb.append(" ");
        }
        return sb.toString().trim();
    }

}
